<template>
  <div class="layer-style">
    <div class="style-card">
      <div class="card-title">
        <span>设置样式</span>

        <i class="el-icon-circle-close close-btn" @click="close"></i>
      </div>

      <div class="card-body">
        <el-select v-model="setStyle.styleId" placeholder="选择图层" popper-class="query-field">
          <el-option v-for="item in styleIdList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </div>
    </div>

    <div class="style-card">
      <div class="card-title">
        <span>填充</span>
      </div>

      <div class="card-body">
        <el-form ref="fillForm" :model="setStyle" :rules="styleRules" label-width="80px">
          <el-form-item label="颜色">
            <el-color-picker v-model="setStyle.fillColor" popper-class="color-popper"></el-color-picker>
            <span class="color-value" v-text="setStyle.fillColor"></span>
          </el-form-item>

          <el-form-item label="透明度">
            <el-slider v-model="setStyle.fillOpacity" :min="0" :max="1" :step="0.1"></el-slider>
            <el-input-number v-model="setStyle.fillOpacity" controls-position="right" :min="0" :max="1" :step="0.1"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div class="style-card border-style">
      <div class="card-title">
        <span>边框</span>
      </div>

      <div class="card-body">
        <el-form ref="borderForm" :model="setStyle" :rules="styleRules" label-width="80px">
          <el-form-item label="是否开启">
            <el-checkbox v-model="setStyle.openBorder"></el-checkbox>
          </el-form-item>

          <el-form-item label="线宽" class="line-width" prop="lineWidth">
            <el-input v-model="setStyle.lineWidth"></el-input>
            <span>px</span>
          </el-form-item>

          <el-form-item label="颜色">
            <el-color-picker v-model="setStyle.borderColor" popper-class="color-popper"></el-color-picker>
            <span class="color-value" v-text="setStyle.borderColor"></span>
          </el-form-item>

          <el-form-item label="透明度">
            <el-slider v-model="setStyle.borderOpacity" :min="0" :max="1" :step="0.1"></el-slider>
            <el-input-number v-model="setStyle.borderOpacity" controls-position="right" :min="0" :max="1" :step="0.1"></el-input-number>
          </el-form-item>

          <el-form-item class="submit-box">
            <el-button type="primary" @click="clickConfirmBtn">确定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validateLineWidth } from '@/assets/js/rules.js';
export default {
  props: ['selectLayerNode'],

  data() {
    return {
      selectNode: null,
      nodeAttribute: null,
      setStyle: {
        styleId: '',
        fillColor: '#247de0',
        fillOpacity: 1,
        openBorder: true,
        lineWidth: '5',
        borderColor: '#247de0',
        borderOpacity: 1
      },
      styleRules: {
        lineWidth: { validator: validateLineWidth, trigger: 'blur' }
      },
      styleIdList: []
    };
  },

  methods: {
    // 获取图层数据列表
    getLengend() {
      const { serverIpPort } = this.nodeAttribute;
      const styleInfo = this.getServerInfo();
      const { serverName, layerName } = styleInfo;
      if (!serverName || !layerName) return;
      this.$get(`${serverIpPort}/mapserver/styleInfo/${serverName}/${layerName}/layer.json`).then(res => {
        if (res && res.length) {
          this.styleIdList = res.map(e => { return e.name; });
        }
      }).catch(err => {
        console.log('获取字段列表报错：', err);
      });
    },

    // 获取服务名称、图层名称
    getServerInfo() {
      const { serverType, serverUrl } = this.nodeAttribute;
      let serverName = '';
      let layerName = '';
      switch (serverType) {
        case 'WMTS':
          const wmtsServerNameReg = new RegExp("(?<=/1.0/).*?(?=/)");
          serverName = serverUrl.match(wmtsServerNameReg) != null ? serverUrl.match(wmtsServerNameReg)[0] : '';
          const wmtsLayerNameReg = new RegExp(`(?<=/${serverName}/).*?(?=/)`);
          layerName = serverUrl.match(wmtsLayerNameReg) != null ? serverUrl.match(wmtsLayerNameReg)[0] : '';
          break;

        case 'WMS':
          const wmsServerNameReg = new RegExp("(?<=/1.1.1/).*?(?=/)");
          serverName = serverUrl.match(wmsServerNameReg) != null ? serverUrl.match(wmsserverNameReg)[0] : '';
          const wmsLayerNameReg = new RegExp(`(?<=/${serverName}/).*?(?=/)`);
          layerName = serverUrl.match(wmsLayerNameReg) != null ? serverUrl.match(wmsLayerNameReg)[0] : '';
          break;

        case 'XYZ':
          const typeReg = new RegExp("(?<=/mapserver/).*?(?=/)");
          const type = serverUrl.match(typeReg) != null ? serverUrl.match(typeReg)[0] : '';
          const mapServerNameReg = new RegExp(`(?<=/${type}/).*(?=/)`);
          const mapLayerNameReg = new RegExp("(?<=styleId=)[^&]*");
          serverName = serverUrl.match(mapServerNameReg) != null ? serverUrl.match(mapServerNameReg)[0] : '';
          layerName = serverUrl.match(mapLayerNameReg) != null ? serverUrl.match(mapLayerNameReg)[0] : '';
          break;

        default:
          break;
      }
      return { serverName, layerName };
    },

    // 点击确定按钮
    clickConfirmBtn() {
      if (!this.setStyle.styleId) return this.message('请选择查询图层');
      this.$refs.borderForm.validate((valid) => {
        if (!valid) return false;

        const { styleId, fillColor, fillOpacity, openBorder, lineWidth, borderColor, borderOpacity } = this.setStyle;
        let colorParms = {
          color: fillColor,
          opacity: fillOpacity,
        };
        if (openBorder) {
          colorParms.strokeWidth = Number(lineWidth);
          colorParms.strokeColor = borderColor;
          colorParms.strokeOpacity = borderOpacity;
        }
        const { serverType, serverUrl } = this.nodeAttribute;
        let controlSchema = {
          id: styleId,
          display: true,
          color: colorParms
        };

        let serverStyleUrl = '';
        switch (serverType) {
          case 'WMTS':
          case 'WMS':
            serverStyleUrl = `${serverUrl}?control={"otherDisplay": false, "layers":[${encodeURIComponent(JSON.stringify(controlSchema))}]}`;
            break;

          case 'XYZ':
            serverStyleUrl = `${serverUrl}&control={"otherDisplay": false, "layers":[${encodeURIComponent(JSON.stringify(controlSchema))}]}`;
            break;

          default:
            break;
        }
        this.nodeAttribute.serverStyleUrl = serverStyleUrl;
        this.nodeAttribute.setStyle = this.setStyle;
        this.selectNode.attribute = this.nodeAttribute;
        this.$emit('setLayerStyle', this.selectNode);
      });
    },

    message(message = '警告', type = 'warning') {
      this.$message({
        message: message,
        type: type
      });
    },

    close() {
      this.$emit('close');
    }
  },

  created() {
    this.selectNode = this.selectLayerNode;
    this.nodeAttribute = this.selectLayerNode.attribute;
    const { setStyle } = this.nodeAttribute;
    if (setStyle) Object.assign(this.setStyle, setStyle);
    this.getLengend();
  }
}
</script>

<style lang="less" scoped>
.layer-style {
  position: fixed;
  left: 306px;
  top: 94px;
  width: 350px;
  background-color: #333333;
  z-index: 999;
  .style-card {
    width: 100%;
    border-bottom: 1px solid #444444;
    .card-title {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      height: 48px;
      padding: 0 0 0 10px;
      box-sizing: border-box;
      background-color: #444444;
      font-size: 14px;
      color: #fff;
      .close-btn {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
        &:hover {
          color: #6fa7e8;
        }
      }
    }

    .card-body {
      /deep/.el-form {
        .el-form-item {
          display: flex;
          align-items: center;
          &:last-child {
            margin-bottom: 0;
          }

          .el-form-item__label {
            font-size: 14px;
            color: #fff;
          }

          .el-form-item__content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: calc(100% - 90px);
            padding: 10px;
            box-sizing: border-box;
            margin-left: 0px !important;
            &::before,
            &::after {
              display: none;
            }

            .el-color-picker {
              width: 100px;
              height: 32px;
              .el-color-picker__trigger {
                width: 100%;
                height: 100%;
              }
            }

            .color-value {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100px;
              height: 32px;
              border: 1px solid #e6e6e6;
              box-sizing: border-box;
              border-radius: 4px;
              font-size: 14px;
              color: #fff;
            }

            .el-slider {
              width: 100px;
              .el-tooltip {
                border: 2px solid #247de0;
              }

              .el-slider__bar {
                background-color: #247de0;
              }
            }

            .el-input-number {
              width: 100px;
              height: 32px;

              .el-input-number__decrease,
              .el-input-number__increase,
              .el-input__inner {
                background-color: rgba(0, 0, 0, 0);
              }

              .el-input-number__decrease,
              .el-input-number__increase {
                &:hover {
                  color: #6fa7e8;
                }
              }

              .el-input__inner {
                border: 1px solid #e6e6e6;
                font-size: 14px;
                color: #fff;
                &:hover {
                  border: 1px solid #e6e6e6;
                }
              }
            }
          }
        }

        .submit-box {
          .el-form-item__content {
            display: flex;
            justify-content: flex-end;
            width: 100%;
            .el-button {
              padding: 6px 12px;
            }
          }
        }
      }

      /deep/.el-select {
        width: 100%;
        padding: 20px;
        box-sizing: border-box;
        .el-input__inner {
          width: 100%;
          background: rgba(0, 0, 0, 0);
          font-size: 14px;
          color: #fff;
        }
      }
    }
  }

  .border-style {
    .card-body {
      /deep/.el-form {
        .el-checkbox {
          .is-checked {
            background-color: #247de0;
          }
        }

        .line-width {
          .el-form-item__content {
            .el-input__inner {
              background-color: rgba(0, 0, 0, 0);
              font-size: 14px;
              color: #fff;
            }
          }

          span {
            margin-left: 10px;
            font-size: 14px;
            font-weight: 600;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>

<style lang="less">
.color-popper {
  .el-color-dropdown__link-btn {
    display: none;
  }
}
</style>